package comnet.chatclient;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.InetAddress;
import java.net.Socket;
import java.net.UnknownHostException;
import java.util.Scanner;

/**
 * @author Mikko Puustinen (mikko.puustinen@gmail.com)
 * @version 0.1, Nov 14, 2007
 */
public class ChatClient {
    private Socket socket = null;
    private static final int DEFAULT_PORT = 3679;
    private int port;
    private String host;
    private InetAddress address;
    private BufferedReader in;
    private Thread out;
    private String line;

    public ChatClient(String host, int port) {
        this.host = host;
        this.port = port;
    }

    public static void main(String[] args) {
        String h = "";
        int p = DEFAULT_PORT;

        if (args.length > 0) {
            h = args[0];
            if (args.length == 2) {
                try {
                    p = Integer.parseInt(args[1]);

                    if (p < 0 || p > 65535) {
                        System.out.println("Error in port number. Valid values: 0 - 65535");
                        return;
                    }
                } catch (NumberFormatException e) {
                    p = DEFAULT_PORT;
                }
            }
        }

        new ChatClient(h, p).runClient();
    }

    /**
     * Runs the client.
     */
    public void runClient() {
        try {
            address = InetAddress.getByName(host);
            socket = new Socket(address, port);
            out = new Thread(new OutputHandler(socket));
            out.setDaemon(true);
            out.start();
            in = new BufferedReader(new InputStreamReader(socket.getInputStream()));

            try {
                while ((line = in.readLine()) != null) {
                    System.out.println(line);
                }
            } catch (IOException e) {
                //
            } finally {
                try {
                    socket.close();
                    in.close();
                } catch (IOException e) {
                    //
                }
            }
        } catch (UnknownHostException e) {
            System.err.println("Unknown host: Could not connect to server.");
        } catch (IOException e) {
            System.err.println("Could not connect to server.");
        }
    }

    private class OutputHandler implements Runnable {
        private PrintWriter out = null;
        private Scanner read = null;

        public OutputHandler(Socket socket) throws IOException {
            out = new PrintWriter(socket.getOutputStream(), true);
        }

        public void run() {
            String line;
            read = new Scanner(System.in);
            try {
                while (true) {
                    line = read.nextLine();
                    out.println(line);
                    out.flush();
                }
            } catch (Exception e) {
                //e.printStackTrace();
            } finally {
                read.close();
                out.close();
            }
        }
    }
}
